function checkAlphaNumeric(txt){
	  return (txt.match( /[A-Za-z][A-Za-z0-9 ]*/ ) == txt);
}

function checkNumeric(txt){
	  return (txt.match( /[0-9]+/ ) == txt);
}

function checkAlphabetic(txt){
	  return (txt.match( /[A-Za-z]+/ ) == txt);
}


var hours = ["0800","0900","1000","1100","1200","1300","1400","1500","1600","1700","1800","1900","2000","2100","2200"];
//a click on a checkbox makes the inner content visible/invisible according to the 
//checkbox checked state, done using a neat trick 
$(".dayBox").click(function(){
	if(this.checked){
		index = this.value;
		$(".dayBoxSpan")[index].style.display = "block";
	}else{
		index = this.value;
		$(".dayBoxSpan")[index].style.display = "none";		
	}
});

$(".beginSelect").change(function(){
	index = (this.id.replace("day","")).replace("Begin","");//index of curent "box"
	$(".endSelect")[index].options.length = 0;//first remove all options
	//insert new options
	tab = this.selectedIndex + 1;
	for(i = 0; i < hours.length - tab;i++){
			$(".endSelect")[index].options[i] = new Option(hours[i+tab], hours[i+tab], (i == 0), false);
	}
});

/*$(".endSelect").change(function(){
	index = (this.id.replace("day","")).replace("End","");//index of curent "box"
	if (($(".beginSelect")[index]).selectedIndex > this.selectedIndex){
		($(".beginSelect")[index]).selectedIndex = this.selectedIndex;
	}
});*/

$("#addBtn").click(function(){
	var can_add = true;
	
	//checking courseName
	if (!checkAlphaNumeric(document.creationForm.courseName.value)){
		$("#courseNameErr").html("Must be alpha-numeric!");
		can_add = false;
	}else{
		$("#courseNameErr").html("");
	}
	
	//checking creditPoints
	if ((!checkNumeric(document.creationForm.creditPoints.value))||(document.creationForm.creditPoints.value == "0")){
		$("#creditPointsErr").html("Must be positive numeric!");
		can_add = false;
	}else{
		$("#creditPointsErr").html("");
	}
	
	//checking capacity
	if ((!checkNumeric(document.creationForm.capacity.value))||(document.creationForm.capacity.value == "0")){
		$("#capacityErr").html("Must be positive numeric!");
		can_add = false;
	}else{
		$("#capacityErr").html("");
	}

	//checking groupNumber
	if (!checkNumeric(document.creationForm.groupNumber.value)){
		$("#groupNumberErr").html("Must be numeric!");
		can_add = false;
	}else{
		$("#groupNumberErr").html("");
	}
	
	//checking description
	if (!checkAlphaNumeric(document.creationForm.description.value)){
		$("#descriptionErr").html("Must be alpha-numeric!");
		can_add = false;
	}else{
		$("#descriptionErr").html("");
	}
	
	//check if a session has been declared
	markers = document.creationForm.dayBox;
	checkedCount = 0;
	for(i = 0; i < markers.length; i++){
		if(markers[i].checked){
			checkedCount++;
		}
	}
	if(checkedCount == 0){
		can_add = false;
		$("#scheduleErr").html("No time has been selected");
	}else{
		$("#scheduleErr").html("");
	}

	if (can_add){
		$.post("addCourse", $("#creationForm").serialize(),function(data){
			//making it work with FF......
			var serializer = new XMLSerializer();
			var xml = serializer.serializeToString(data);
			data = xml.replace("<firefoxsucks>","").replace("</firefoxsucks>","");
			//real begining fo function
			if (data == "added"){
				//do the same as when 'home' is clicked
				$("#mng_courses").click();
			}else if(data == "alreadyExists"){
	    		$("#courseNameErr").html("Such course already exists");
	    		$("#scheduleErr").html("");
	    		$("#courseNameErr").css("color","#FF0000");
			}else{
				$("#scheduleErr").html("There is a collision with another course");
			}
		});
	}
	
});